package com.cheer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cheer.dao.ExMatchUserDao;
import com.cheer.entity.ExMatchUserEntity;
import com.cheer.param.SendInfoUpdateParam;
import com.cheer.param.UserUpdateParam;
import com.cheer.service.AuthService;
import com.cheer.service.ExAuthService;
import com.cheer.vo.ExMatchUserVO;
import com.cheer.vo.SendInfoVO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


@Service
public class ExAuthServiceImpl implements ExAuthService {

    @Autowired
    private ExMatchUserDao exMatchUserDao;


    @Override
    public ExMatchUserVO info(ExMatchUserEntity user) {

        if (user == null) {
            return null;
        }

        ExMatchUserVO exMatchUserVO = new ExMatchUserVO();
        BeanUtil.copyProperties(user, exMatchUserVO);

        if (StringUtils.isNotBlank(user.getProvince()) && StringUtils.isNotBlank(user.getCity())
                && StringUtils.isNotBlank(user.getRegion()) && StringUtils.isNotBlank(user.getAddress())) {
            if (StringUtils.equals(user.getProvince(), user.getCity())) {
                exMatchUserVO.setFullAddress(StringUtils.join(Lists.newArrayList(user.getCity(),
                        user.getRegion(), user.getAddress()), " "));
            } else {
                exMatchUserVO.setFullAddress(StringUtils.join(Lists.newArrayList(user.getProvince(), user.getCity(),
                        user.getRegion(), user.getAddress()), " "));
            }
        }

        return exMatchUserVO;
    }

    @Override
    public void updateUser(UserUpdateParam userUpdateDTO, Long userId) {
        ExMatchUserEntity user = new ExMatchUserEntity();
        user.setId(userId);
        BeanUtil.copyProperties(userUpdateDTO, user);
        exMatchUserDao.updateById(user);
    }

    @Override
    public SendInfoVO getSendInfo(ExMatchUserEntity user) {

        if (user == null) {
            return new SendInfoVO();
        }
        return new SendInfoVO()
                .setPhone(user.getPhone())
                .setProvince(user.getProvince())
                .setCity(user.getCity())
                .setRegion(user.getRegion())
                .setAddress(user.getAddress());
    }

    @Override
    public void updateSendInfo(SendInfoUpdateParam sendInfoUpdateDTO, Long userId) {
        ExMatchUserEntity user = new ExMatchUserEntity();
        BeanUtils.copyProperties(sendInfoUpdateDTO, user);
        user.setId(userId);
        exMatchUserDao.updateById(user);
    }

}
